import logging
import time
import os
from logging.handlers import TimedRotatingFileHandler


class LoggingMethod(object):
    def __init__(self, name):
        """
        将日志输出到文件
        :param name:
        """
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s-%(name)s-%(levelname)s-%(message)s', filemode='a')
        self.logger = logging.getLogger(name)  # 实例化logger对象
        self.logger.setLevel(logging.DEBUG)
        now = time.strftime('%Y-%m-%d_%H_%M_%S')
        filename = 'D:/commercial_opportunity_zhu' + '\\test_log\\' + now + '-' + name + '.log'
        fh = TimedRotatingFileHandler(filename,   when='H', encoding="utf-8")
        # fh = logging.FileHandler(filename, encoding='utf-8')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s'))
        self.logger.addHandler(fh)
        # logging.basicConfig(level=logging.DEBUG,  # 控制台打印的日志级别
        #                     filename=
        #                     'log_new.log',  # 将日志写入log_new.log文件中
        #                     filemode='a',  # 模式，有w和a，w就是写模式，每次都会重新写日志，覆盖之前的日志
        #                     # a是追加模式，默认如果不写的话，就是追加模式
        #                     format=
        #                     '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
        #                     # 日志格式
        #                     )
        # self.logger = logging.getLogger(name)  # 实例化logger对象


    def get_logger(self):
        return self.logger


if __name__ == '__main__':
    logger = LoggingMethod(__name__).get_logger()
    logger.info('这是test_login 中的init方法')
    logger.info('这是test_login 中的init方法')
    logger.info('这是test_login 中的init方法')
